package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.AssessmentIndicatorService;
import com.lmx.repoDomain.tmodel.AssessmentIndicator;

/**
 * 考核指标
 *
 * @author ytche
 * @date 2017年6月28日
 *
 */
@Component
@JsonApiResourceRepository(value = AssessmentIndicator.class)
public class AssessmentIndicatorRepository extends BaseRepository<AssessmentIndicator> {
	private final String DEFAULT_YES = "1";

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(AssessmentIndicator.class);
	}

	@Autowired
	AssessmentIndicatorService service;

	@Override
	@JsonApiFindAll
	public Iterable<AssessmentIndicator> findAll(final QueryParams requestParams) {
		Map<String, String> filtersMap = CommonUtil.getKeyMapByFilter(requestParams);
		if (filtersMap.containsKey("forTemp")) {
			Integer assessmentDate = CommonUtil.getWeekBegin(new Date(), true);
			if (filtersMap.containsKey("assessmentDate")
					&& StringUtils.isNumeric(filtersMap.get("assessmentDate"))) {
				assessmentDate = new Integer(filtersMap.get("assessmentDate"));
			}
			return this.service.findForTempAssessment(assessmentDate);
		}
		if (filtersMap.containsKey("forTenant")
				&& filtersMap.get("forTenant").equals(this.DEFAULT_YES)) {
			return this.service.findNotDelete("tenant.id", ActionContextHolder.getContext()
					.getTenantId());
		}
		return super.findAll(requestParams);
	}
}
